<?php
namespace app\controllers;

use app\common\corelib\CacheHelper;
use app\common\WxUtil;
use app\logic\CourseLogic;
use app\logic\OrderLogic;
use app\logic\UserLogic;
use app\models\User;
//前端课程接口
class CourseController extends BaseController
{
    //获取班级
    public function actionGetClass(){
        $token = $this->getQueryparams('token','');
        if(!User::findIdentityByAccessToken($token)){
            $this->error('401','请登录');
        }else{
            $courseLogic = new CourseLogic();
            $result = $courseLogic->getClass();
            $this->response($result);
        }
    }
    //获取课程信息
    public function actionGetInfo(){
        $open_id = $this->getQueryparams('open_id',"");
        $token = $this->getQueryparams('token',"");
        $course_type = $this->getQueryparams('course_type',0);
        $extend_type = $this->getQueryparams('extend_type',0);
        if($course_type==7){
            $user = CacheHelper::get('user_token_'.$token);
            $userLogic = new UserLogic();
            $usertemp = $userLogic->findByOpenid($open_id);
            if($usertemp['user_id']!=$user['user_id']){
                $this->error('401','没有权限');
            }
        }
        $courseLogic = new CourseLogic();
        $course = $courseLogic->getCourse($course_type,$extend_type);
        $course['course_pic'] = \Yii::$app->params['upload']['upload_domain'].$course['course_pic'];
        $orderLogic = new OrderLogic();
        $chapter = $courseLogic->getChapter($course['course_id']);
        if($course_type==7){
            foreach ($chapter AS $key=>&$value){
                $value['chapter_pic'] = \Yii::$app->params['upload']['upload_domain'].$value['chapter_pic'];
                $value['chapter_test_video'] = \Yii::$app->params['upload']['upload_domain'].$value['chapter_test_video'];
                $value['chapter_video'] = \Yii::$app->params['upload']['upload_domain'].$value['chapter_video'];
                $have_buy = $orderLogic->haveBuy($open_id,$course['course_id'],$value['chapter_id']);
                if(!$have_buy){
                    unset($value['chapter_video']);
                }
            }
        }else{
            $have_buy = $orderLogic->haveBuy($open_id,$course['course_id']);
            foreach ($chapter AS $key=>&$value){
                $value['chapter_pic'] = \Yii::$app->params['upload']['upload_domain'].$value['chapter_pic'];
                $value['chapter_test_video'] = \Yii::$app->params['upload']['upload_domain'].$value['chapter_test_video'];
                $value['chapter_video'] = \Yii::$app->params['upload']['upload_domain'].$value['chapter_video'];
                if(!$have_buy){
                    unset($value['chapter_video']);
                }

            }
            if($have_buy){
                $course['have_buy'] = true;
            }else{
                $course['have_buy'] = false;
            }
        }

        $history = $courseLogic->getLastChapter($course['course_id']);
        if($history){
            $course['record_id'] = $history['id'];
        }else{
            $course['record_id'] = 0;
        }

        $course['chapter'] = $chapter;
        $course['chapter_nums'] = count($chapter);
        $this->response($course);
    }
    //获取评论
    public function actionGetComment(){
        $course_id = $this->getQueryparams('course_id',0);
        $page = $this->getQueryparams('page',1);
        $limit = $this->getQueryparams('limit',10);
        $courseLogic = new CourseLogic();
        $comment = $courseLogic->getComment($course_id,$page,$limit,$count);
        foreach($comment AS $key=>&$value){
            $value['add_time'] = date('Y-m-d',strtotime($value['add_time']));
            $value['nick_name'] = base64_decode($value['nick_name']);
        }
        $this->response(['comment_num'=>$count,'comment'=>$comment]);
    }
    //添加评论
    public function actionAddComment(){
        $open_id = $this->getQueryparams('open_id','');
        $course_id = $this->getQueryparams('course_id',0);
        $comment_score = $this->getQueryparams('comment_score',5);
        $comment_content = $this->getQueryparams('comment_content','');
        $courseLogic = new CourseLogic();
        $orderLogic = new OrderLogic();
        $have_buy = $orderLogic->haveBuy($open_id,$course_id);
        if($have_buy){
            $courseLogic->addComment($open_id,$course_id,$comment_score,$comment_content);
            $this->response();
        }

    }
    //获取分享信息用于jssdk
    public function actionGetShareInfo(){
        $open_id = $this->getQueryparams('open_id','');
        $paper_id = $this->getQueryparams('paper_id',0);
        $url = $this->getQueryparams('url','');
        $answer_no = $this->getQueryparams('answer_no','');
        $userLogic = new UserLogic();
        $courseLogic = new CourseLogic();
        $user = $userLogic->findByOpenid($open_id);
        $result['head_img'] = $user['head_img'];
        $result['nick_name'] = base64_decode($user['nick_name']);
        $result['share_num'] = $user['share_num'];
        $result['time'] = date('Y-m-d');

        $paper = $courseLogic->getPaper($paper_id);
        if($paper['paper_type']==1 || $paper['paper_type']==2 || $paper['paper_type']==3 || $paper['paper_type']==4 || $paper['paper_type']==5 || $paper['paper_type']==6){
            if($paper['paper_type']==3){
                $sentence = $courseLogic->getSentence(0,1);
            }elseif($paper['paper_type']==5){
                $sentence = $courseLogic->getSentence(0,3);
            }elseif($paper['paper_type']==6){
                $sentence = $courseLogic->getSentence(0,6);
            }elseif($paper['paper_type']==1){
                $sentence = $courseLogic->getSentence(0,8);
            }else{
                $sentence = $courseLogic->getSentence(0);
            }

        }else{
            if($paper['paper_type']==8){
                $sentence = $courseLogic->getSentence(1,2);
            }elseif($paper['paper_type']==9){
                $sentence = $courseLogic->getSentence(1,4);
            }elseif($paper['paper_type']==7){
                $sentence = $courseLogic->getSentence(1,5);
            }elseif($paper['paper_type']==2){
                $sentence = $courseLogic->getSentence(1,9);
            }else{
                $sentence = $courseLogic->getSentence(1);
            }
        }

        $result['title'] = $courseLogic->getPaperTitle($paper_id);
        $result['sentence'] = $sentence;
        $result['success'] = $courseLogic->getCorrect($user['user_id'],$paper_id,$answer_no);
        $result['fail'] = $courseLogic->getNotCorrect($user['user_id'],$paper_id,$answer_no);
        $result['appId'] = \Yii::$app->params['wx']['appid'];
        $result['timestamp'] = time();
        $result['nonceStr'] = self::GetRandStr(16);
        $wxUtil = new WxUtil();
        $jsapiTicket = $wxUtil->getJsSdkTicket();
        $string = "jsapi_ticket=$jsapiTicket&noncestr=".$result['nonceStr']."&timestamp=".$result['timestamp']."&url=$url";
        $signature = sha1($string);
        $result['signature'] = $signature;
        if($user['study_time']){
            //$hour=floor($user['study_time']%86400/3600);
            $hour=floor($user['study_time']/3600);
            //$minute=floor($user['study_time']%86400/60);
            $minute=floor($user['study_time']%3600/60);
            $result['study_time'] = $hour.'小时'.$minute.'分';
        }else{
            $result['study_time'] = 0;
        }

        $this->response($result);

    }

    private static function GetRandStr($length)
    {
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $len = strlen($str) - 1;
        $randstr = '';
        for ($i = 0; $i < $length; $i++) {
            $num = mt_rand(0, $len);
            $randstr .= $str[$num];
        }
        return $randstr;
    }
    //获取章节
    public function actionGetChapter(){
        $chapter_id = $this->getQueryparams('chapter_id',0);
        $open_id = $this->getQueryparams('open_id',"");
        $courseLogic = new CourseLogic();
        $chapter = $courseLogic->getChapterById($chapter_id);
        $chapter['chapter_pic'] = \Yii::$app->params['upload']['upload_domain'].$chapter['chapter_pic'];
        //$chapter['chapter_test_video'] = $chapter['chapter_test_video']?\Yii::$app->params['upload']['upload_domain'].$chapter['chapter_test_video']:\Yii::$app->params['upload']['upload_domain'].$chapter['chapter_video'];
        $chapter['chapter_test_video'] = \Yii::$app->params['upload']['upload_domain'].$chapter['chapter_test_video'];
        $chapter['chapter_video'] = \Yii::$app->params['upload']['upload_domain'].$chapter['chapter_video'];
        $orderLogic = new OrderLogic();
        $course = $courseLogic->getById($chapter['course_id']);
        if($course['course_type']==7){
            $have_buy = $orderLogic->haveBuy($open_id,$chapter['course_id'],$chapter_id);
        }else{
            $have_buy = $orderLogic->haveBuy($open_id,$chapter['course_id']);
        }

        $chapters = $courseLogic->getChapter($chapter['course_id']);
        if($course['course_type']==7){
            foreach ($chapters AS $key=>&$value){
                $value['chapter_pic'] = \Yii::$app->params['upload']['upload_domain'].$value['chapter_pic'];
                //$value['chapter_test_video'] = $value['chapter_test_video']?\Yii::$app->params['upload']['upload_domain'].$value['chapter_test_video']:\Yii::$app->params['upload']['upload_domain'].$value['chapter_video'];
                $value['chapter_video'] = \Yii::$app->params['upload']['upload_domain'].$value['chapter_video'];
                $have_buys = $orderLogic->haveBuy($open_id,$course['course_id'],$value['chapter_id']);
                if(!$have_buys){
                    unset($value['chapter_video']);
                }
            }
        }else{
            $have_buys = $orderLogic->haveBuy($open_id,$course['course_id']);
            foreach ($chapters AS $key=>&$value){
                $value['chapter_pic'] = \Yii::$app->params['upload']['upload_domain'].$value['chapter_pic'];
                //$value['chapter_test_video'] = $value['chapter_test_video']?\Yii::$app->params['upload']['upload_domain'].$value['chapter_test_video']:\Yii::$app->params['upload']['upload_domain'].$value['chapter_video'];
                $value['chapter_video'] = \Yii::$app->params['upload']['upload_domain'].$value['chapter_video'];
                if(!$have_buys){
                    unset($value['chapter_video']);
                }
            }
        }


        if($have_buy){
            $have_buy = true;
        }else{
            $have_buy = false;
        }


        $chapter['other_chapter'] = $chapters;
        $chapter['chapter_nums'] = count($chapters);
        if($have_buy){
            $userLogic = new UserLogic();
            $userLogic->addStudyHistory($open_id,1,$chapter_id);
        }else{
            unset($chapter['chapter_video']);
        }
        $chapter['have_buy'] = $have_buy;

        $this->response($chapter);


    }

}
